package com.alan.sso.client.config;

import com.alan.sso.share.bean.Constant;
import com.alan.sso.share.filter.SsoAuthenticationFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Auther: Alan liu
 * @Date: 2018/12/20 17:11
 * @Description: SsoFilterConfig
 */
@Configuration
public class SsoFilterConfig {
    @Value("${ssoUrl}")
    private String ssoUrl;
    @Value("${sso.appName}")
    private String ssoAppName;
    @Value("${sso.appKey}")
    private String ssoAppKey;

    @Bean
    public FilterRegistrationBean filterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new SsoAuthenticationFilter());
        registration.addUrlPatterns("/*");
        /** 申请的服务名称 */
        registration.addInitParameter(Constant.K_APPNAME, ssoAppName);
        registration.addInitParameter(Constant.K_LOGINURL, ssoUrl + "/sso/login");
        registration.addInitParameter(Constant.K_VALIDATEURL, ssoUrl + "/sso/validate");
        registration.addInitParameter(Constant.K_LOGOUTURL, ssoUrl + "/sso/logout");
        /** 申请的cookie 加密密码。 加密方式： AES */
        registration.addInitParameter(Constant.K_APPKEY, ssoAppKey);
        /** 设置登录cookie的过期时间，单位秒（s） */
        registration.addInitParameter("expireTime", "3600");
        /** 设置排除在外的URL , 以逗号分隔/zalabs/demo/login */
        String ssoExcludes = "/sso/login,/sso/logout,/sso/loginSubmit,/sso/validate,/sso/auth/api/role,/sso/toChangePassword,/sso/changePassword,/health,/open/v1/commission/*";
        registration.addInitParameter("exclusive", ssoExcludes
                + ",/js/*,/login/index,/bootstrap/*,/build/*,/documentation/*,/pages/*,/plugins/*,/dist/*");
        registration.setName("authenticationFilter");
        registration.setOrder(1);
        return registration;
    }
}
